(function ($, window, document, undefined) {
    console.log('macro_uploader_ctl.js');
    var blobArr = [];
    $.Uploader = $.Class.extend({
        init: function (element, options) {
            this.element = element,
                this.default = {}
            this.options = $.extend(true, this.default, options);
        },
        imageResize: function (file, captureFieldName) {
            if (maxSize.width == 0)
                return;
            var fieldName = this.options.fieldName;
            var fileReader = new FileReader();
            fileReader.onload = function () {
                console.log('imageResize onload');
                var base64Str = this.result;
                if (/\/(?:jpeg|png|gif|jpg)/i.test(file.type)) {
                    var IMG = new Image();
                    IMG.src = base64Str;
                    IMG.onload = function () {
                        var w = this.naturalWidth, h = this.naturalHeight, resizeW = 0, resizeH = 0;
                        if (w > maxSize.width || h > maxSize.height) {
                            var multiple = Math.max(w / maxSize.width, h / maxSize.height);
                            resizeW = w / multiple;
                            resizeH = h / multiple;
                            var canvas = document.createElement('canvas'),
                                ctx = canvas.getContext('2d');
                            if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
                                console.log('imageResize ios');
                                canvas.width = resizeW;
                                canvas.height = resizeH;

                                var orient = getPhotoOrientation(IMG);
                                switch (orient) {
                                    case 6://需要顺时针（向左）90度旋转
                                        rotateImg(IMG, 'left', canvas);
                                        break;
                                    case 8://需要逆时针（向右）90度旋转
                                        rotateImg(IMG, 'right', canvas);
                                        break;
                                    case 3://需要180度旋转
                                        rotateImg(IMG, 'right', canvas);//转两次
                                        rotateImg(IMG, 'right', canvas);
                                        break;
                                    default:
                                        ctx.drawImage(IMG, 0, 0, resizeW, resizeH);
                                }
                            } else {
                                console.log('imageResize android');
                                canvas.width = resizeW;
                                canvas.height = resizeH;
                                ctx.drawImage(IMG, 0, 0, resizeW, resizeH);
                            }
                            base64Str = canvas.toDataURL('image/jpeg', maxSize.level);
                        }

                        var imgBox = '<li class="img-box" fId="0" field="' + captureFieldName + '"><img class="img-box-img" src="' + base64Str + '"/>';
                        imgBox += '<span class="btn-del-img"><img class="att_del" isImgBox="true" src="' + getContextPath() + '/static/images/btn_del.png"></span></li>';
                        jQuery("[uploaderCtl='" + fieldName + "']").append(imgBox);

                        var blob = convertBlob(window.atob(base64Str.split(',')[1]));
                        var blobObj = {"fname": file.name, "blob": blob, "field": captureFieldName};
                        blobArr.push(blobObj);
                        // console.info(blobArr);
                    }
                } else {
                    console.log('imageResize captureFieldName', captureFieldName);
                    var imgBox = '<li class="img-box" fId="0" field="' + captureFieldName + '" style="padding:5px">' + file.name;
                    imgBox += '<span class="btn-del-img"><img class="att_del" isImgBox="true" src="' + getContextPath() + '/static/images/btn_del.png"></span></li>';
                    jQuery("[uploaderCtl='" + fieldName + "']").append(imgBox);

                    var blob = convertBlob(window.atob(base64Str.split(',')[1]));
                    var blobObj = {"fname": file.name, "blob": blob, "field": captureFieldName};
                    blobArr.push(blobObj);
                }
            };
            fileReader.readAsDataURL(file);
        },
        initUploaderCtl: function () {
            var self = this;

            var fieldName = this.options.fieldName;
            console.log('fieldName', fieldName);
            var captureFile = document.getElementById(fieldName);
            if (captureFile != null) {
                captureFile.onchange = function () {
                    if (!this.files.length) return;
                    var files = Array.prototype.slice.call(this.files);
                    if (files.length > 9) {
                        mui.toast("最多同时只可上传9个文件");
                        return;
                    }

                    files.forEach(function (file, i) {
                        console.log('file', file);
                        self.imageResize(file, fieldName);
                    });
                }

                console.log('fieldName', fieldName);
                jQuery("[uploaderCtl='" + fieldName + "']").on("tap", ".uploader-capture-btn", function () {
                    var cap = jQuery("#" + fieldName).get(0);
                    console.log('cap', cap);
                    cap.click();
                });
            }
        },
        getBlobArr: function () {
            return blobArr;
        }
    })
})(mui, window, document)